package com.hk.housekeeping.controller;

import com.hk.housekeeping.entity.ManagerInfo;
import com.hk.housekeeping.entity.RegisterManager;
import com.hk.housekeeping.entity.UpdateManager;
import com.hk.housekeeping.service.ManagerServ;
import com.hk.housekeeping.utils.PageUtils;
import com.hk.housekeeping.utils.ResponseCode;
import com.hk.housekeeping.utils.ResponseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reqManager")
public class ManagerController {
    @Autowired
    private ManagerServ managerServ;

    //登录
    @PostMapping("/login")
    public ResponseEntity<Integer> login(@RequestParam String username, @RequestParam String password) {
        ResponseEntity<Integer> entity = null;
        try {
            entity = managerServ.login(username, password);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.MANAGER_LOGIN_ERROR);
        }

        return entity;
    }

    //注册（新增一条管理员的信息）
    @PostMapping("/register")
    public ResponseEntity<Integer> registerManager(@RequestBody RegisterManager registerManager) {
        ResponseEntity<Integer> entity = null;
        try {
            entity = managerServ.registerManager(registerManager);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.MANAGER_INSERT_ERROR);
        }
        return entity;
    }

    //删除（删除一条管理员的信息）
    @DeleteMapping("/delManager")
    public ResponseEntity<Integer> delManager(@RequestParam("id") int mid) {
        ResponseEntity<Integer> entity = null;
        try {
            entity = managerServ.deleteManager(mid);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.MANAGER_DELETE_ERROR);
        }
        return entity;
    }

    //编译（修改管理员的信息）
    @PutMapping("/edit")
    public ResponseEntity<Integer> editManager(@RequestBody UpdateManager updateManager) {
        ResponseEntity<Integer> entity = null;
        try {
            entity = managerServ.editManager(updateManager);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.MANAGER_EDIT_ERROR);
        }
        return entity;
    }

    //分页展示
    @GetMapping("/pageShowManager")
    public ResponseEntity<PageUtils<List<ManagerInfo>>> pageSelect(@RequestParam(value = "page",defaultValue = "1") int currentPage, @RequestParam(value = "size", defaultValue = "5") int pageSize) {
        ResponseEntity<PageUtils<List<ManagerInfo>>> entity = null;
        try {
            entity = managerServ.pageSelect(currentPage, pageSize);
        } catch (Exception e) {
            entity = ResponseEntity.error(ResponseCode.MANAGER_PAGE_SELECT_ERROR);
        }

        return entity;
    }
}
